﻿<?php 


	class yunque{

		public $auth;

		public function getyqToke(){

			$result=self::php_do_Url_POST(
				 'https://api.yunque360.com/v1/worker/account/login'
				,["password"=>"591591","fullname"=>"1005@qmjj","identifier"=>"1005@qmjj"]
				,['Authorization: '.$this->$auth]);

			return $result;

		}

		public function userLists($page=1,$pagesize=10){


			$result=self::php_do_Url_POST(
				 'https://api.yunque360.com/v1/worker/wxuser/index'
				,['page'=>$page,'order_by'=>'last_event_time','pagesize'=>$pagesize]
				,['Authorization: '.$this->$auth]);



			return $result;

		}

		public function userLianTianJiLu($open_id){


				$result1=self::php_do_Url_POST(
						 'https://api.yunque360.com/v1/company/conversation/wechat-history'
						,['open_id'=>$open_id]
						,['Authorization: '.$this->$auth]);


				return $result1;

		}


		public function remark($open_id){

				$result2=self::php_do_Url_GET(
				 'https://api.yunque360.com/v1/worker/user/info'
				,['uid'=>$open_id]
				,['Authorization: '.$this->$auth]);


				$rem=$result2['result'];

					echo '<pre>';
						var_dump($result2['result']);
					echo '</pre>';

				if($rem['mobile']!==null){

					$result=['showname'=>$rem['showname'],'mobile'=>$rem['mobile'],'last_event_time'=>$rem['last_event_time'],'open_id'=>$rem['open_id']];

					return $result;

				}else{

					return false;

				}


		}

		public function insertDb($query){


	        $host='localhost';
	        $user='root';
	        $password='root';
	        $database='weizandata';
	        $link=mysqli_connect($host, $user, $password, $database);
	        $link->query("set names utf8");
	        $result=mysqli_query($link, $query);
	        //@$arr=mysqli_fetch_all($result, 1);

	        //echo mysqli_error($link);

	        return ["result"=>$result,"row"=>mysqli_affected_rows($link),'erro'=>mysqli_error($link)];



		}

	    public function  php_do_url_POST($url,$params,$headers){ //POST get

			$ch = curl_init ();

			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
			curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
			curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSLVERSION, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

			$json_string = curl_exec ( $ch );
			$data = json_decode($json_string, true);
			curl_close ( $ch );
			return $data;
		}

		public function php_do_Url_GET($url,$params=[],$headers=[]){ //Get get

			$url = "{$url}?" . http_build_query ($params);
			//echo $url;
			$ch = curl_init ();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
			curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
			//curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSLVERSION, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


			$json_string = curl_exec ( $ch );

			$data = json_decode($json_string, true);

			curl_close ( $ch );

			return $data;
		}

		function selecToke($name){  //获取百应密钥

			$qure="SELECT toke FROM toke WHERE name_i ='{$name}';";
			$host='localhost';
			$user='root';
			$password='root';
			$database='weizandata';
			$link=mysqli_connect($host, $user, $password, $database);
			$link->query("set names utf8");
			$result=mysqli_query($link, $qure);
			$arr=mysqli_fetch_all($result, 1);
			return $arr[0]['toke'];

		}


		function selectCountData(){


			$limit=$_POST['limit'];

			$offset=($_POST['page']-1)*$limit;

			$qure="SELECT DATE,COUNT,(SELECT COUNT(*) FROM(SELECT COUNT(*) AS COUNT FROM wxgongzhonghaophone GROUP BY LEFT(last_event_time,10)) a) AS limits FROM ( SELECT LEFT(last_event_time,10) AS DATE ,COUNT(*) AS COUNT FROM wxgongzhonghaophone GROUP BY LEFT(last_event_time,10) ORDER BY DATE DESC ) a  LIMIT {$limit} OFFSET {$offset};";

			//echo $qure.'<br>';

			$host='localhost';
			$user='root';
			$password='root';
			$database='weizandata';
			$link=mysqli_connect($host, $user, $password, $database);
			$link->query("set names utf8");
			$result=mysqli_query($link, $qure);
			$arr=mysqli_fetch_all($result, 1);
			$jsonData=json_encode($arr);

			//var_dump($arr[0]['limits']);

			$json = json_encode(array(
					"code"=>0,
					"count"=>$arr[0]['limits'],  
					"data"=>$arr ),
					JSON_UNESCAPED_UNICODE);
	
	
			mysqli_close($link);


			return $json;



		}

		function selectWork_lastData(){


			$limit=$_POST['limit'];

			$offset=($_POST['page']-1)*$limit;

			$qure="SELECT DATE,last_worker,COUNT,(SELECT COUNT(*) FROM(SELECT LEFT(last_event_time,10) AS DATE ,last_worker ,COUNT(*) AS COUNT FROM wxgongzhonghaophone GROUP BY LEFT(last_event_time,10),last_worker ORDER BY DATE DESC) a) AS limits FROM ( SELECT LEFT(last_event_time,10) AS DATE ,last_worker ,COUNT(*) AS COUNT FROM wxgongzhonghaophone GROUP BY LEFT(last_event_time,10),last_worker ORDER BY DATE DESC) a  LIMIT {$limit} OFFSET {$offset};";

			//echo $qure.'<br>';

			$host='localhost';
			$user='root';
			$password='root';
			$database='weizandata';
			$link=mysqli_connect($host, $user, $password, $database);
			$link->query("set names utf8");
			$result=mysqli_query($link, $qure);
			$arr=mysqli_fetch_all($result, 1);
			$jsonData=json_encode($arr);

			//var_dump($arr[0]['limits']);

			$json = json_encode(array(
					"code"=>0,
					"count"=>$arr[0]['limits'],  
					"data"=>$arr ),
					JSON_UNESCAPED_UNICODE);
	
	
			mysqli_close($link);


			return $json;



		}





	}


 ?>
